package net.cyndeline.components.agent

import net.cyndeline.entitySystem.Component

/**
 * Stores the current view of the map that the player has. This only
 * concerns field of view as it is used to reveal graphical parts of the map,
 * and should only be present on the player entity.
 */
trait MapViewComponent extends Component {
  
  /**
   * Returns every tile position the entity can currently see.
   */
  def fieldOfView: Set[(Int, Int)]
  
  /**
   * Updates the current field of view. Any positions present in the
   * previous view (but not this one) will be added to fog of war if they
   * don't already exists.
   */
  def fieldOfView_=(newFov: Set[(Int, Int)])
  
  /**
   * Returns every tile position the entity has seen at some point,
   * but no longer sees. Does not contain field of view.
   */
  def fogOfWar: Set[(Int, Int)]
  
  /**
   * Sets a new fog of war. The final set will be the fog of war set
   * minus the current field of view.
   */
  def fogOfWar_=(newFow: Set[(Int, Int)])

}

class MapView extends MapViewComponent {
  private var fov = Set[(Int, Int)]()
  private var fow = Set[(Int, Int)]()
  
  override def fieldOfView = fov
  
  override def fieldOfView_=(newFov: Set[(Int, Int)]) {
    
    /* Every position that is in the old view, but not the new one. */
    var difference = fov diff newFov
    fov = newFov
    
    fogOfWar = (fogOfWar | difference)
  }
  
  override def fogOfWar = fow
  
  override def fogOfWar_=(newFow: Set[(Int, Int)]) {
    fow = newFow diff fieldOfView
  }
  
  override def copy = {
    val copy = new MapView()
    copy.fieldOfView = fov
    copy.fogOfWar = fow
    copy
  }
}